import { Request, Response, NextFunction } from "express";
import { connection } from "@/services/database";
import { createRandomNumber } from "@/utils/random";
import { generateInsertFields } from "@/utils/sql";

interface Params {
	username: string;
	password: string;
	phone_number?: string | null;
}
export default async (request: Request, response: Response, next: NextFunction) => {
	try {
		const params = request.body as unknown as Params;
		const user_id = createRandomNumber(8);
		(await connection.execute("INSERT INTO user " + generateInsertFields({ user_id, ...params }), {
			user_id,
			...params,
		})) as any;
		response.json({ data: null, message: "成功" });
	} catch (error) {
		response.statusCode = 400;
		next(error);
	}
};
